package com.yuan.springcloud.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuan.springcloud.common.exception.ErrorEnum;
import com.yuan.springcloud.common.utils.ValidUtil;
import com.yuan.springcloud.common.web.bean.Result;
import com.yuan.springcloud.user.bean.dto.UserInfoDTO;
import com.yuan.springcloud.user.bean.po.UserPO;
import com.yuan.springcloud.user.bean.vo.UserQueryVO;
import com.yuan.springcloud.user.bean.vo.UserSignVO;
import com.yuan.springcloud.user.bean.vo.UserVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author YouYuan
 * @date 2020/8/2 23:29
 */
@Api(value = "用户接口",tags = "用户")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController extends BaseController{

    // region 用户查询

    @GetMapping("/info")
    @ApiOperation(value = "获取用户信息",notes = "使用用户id获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "int", example = "1")
    })
    public String userInfo(Integer id) {
        return "user: YouYuan" + "id: " + id;
    }

    @GetMapping("/queryAll")
    @ApiOperation(value = "查询所有用户",notes = "使用过滤条件查询所有用户")
    public Result<List<UserVO>> queryAllUserList() {
        List<UserPO> list = userService.queryAllUserList();
        return Result.success(transform.user.po2vo(list));
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询用户列表")
    public Result<IPage<UserVO>> queryUserList(Page page) {
        IPage<UserPO> userIPage = userService.queryUserList(page);
        return Result.success(userIPage.convert(transform.user::po2vo));
    }

    @PostMapping("/query")
    @ApiOperation(value = "查询用户列表")
    public Result<List<UserVO>> queryUser(@RequestBody UserQueryVO queryParam) {
        List<UserPO> userList = userService.queryUserList(queryParam);
        return Result.success(transform.user.po2vo(userList));
    }

    /**
     * GetMapping不支持RequestBody，需要使用PostMapping
     * @param param
     * @return
     */
    @PostMapping("/listById")
    @ApiOperation(value = "根据userId查询用户列表")
    public Result<List<UserVO>> queryUserListById(@RequestBody UserQueryVO param) {
        List<UserPO> userList = userService.queryUserListById(param.getUserIdList());
        return Result.success(transform.user.po2vo(userList));
    }

    @GetMapping("/infoList")
    @ApiOperation(value = "查询用户信息列表")
    public Result<List<UserInfoDTO>> queryUserInfoList() {
        List<UserInfoDTO> infoDTOList = userService.queryUserInfoList();
        return Result.success(infoDTOList);
    }

    // endregion

    @GetMapping("/login/{userId}")
    @ApiOperation(value = "用户登录")
    public Result login(@PathVariable Long userId) {
        int rs = userService.login(userId);
        return Result.success(rs);
    }
    @GetMapping("/logout/{userId}")
    @ApiOperation(value = "用户登出")
    public Result logout(@PathVariable Long userId) {
        int rs = userService.logout(userId);
        return Result.success(rs);
    }

    @GetMapping("/queryOnlineList")
    @ApiOperation(value = "查询在线用户列表")
    public Result<List<Integer>> queryOnlineList() {
        List<Integer> onlineList = userService.queryOnlineList();
        return Result.success(onlineList);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新用户信息",notes = "根据id更新用户信息")
    public Result<Integer> update(@RequestBody @Valid UserVO user) {
        return Result.success(userService.update(user));
    }

    @PostMapping("/sign")
    @ApiOperation(value = "用户签到",notes = "记录用户位置进行签到")
    public Result<Integer> sign(@RequestBody @Valid UserSignVO userSignVO) {
        return Result.success(userService.sign(userSignVO));
    }

    @GetMapping("/error")
    @ApiOperation(value = "异常")
    public Result error() {
        return Result.error(ErrorEnum.PARAMS_VALID_ERR);
    }

}
